
package shared.units;

import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Cylinder;

public class Bullet {

	Geometry geo;
	RigidBodyControl control;
	Node node;

	BulletAppState bulletAppState;

	private boolean isInUse = false;
	
	public Bullet(BulletAppState bulletAppState, Node node, Material mat) {
		this.node = node;
		this.bulletAppState = bulletAppState;
		init(mat);
	}

	protected void init(Material mat) {

		Cylinder cyl = new Cylinder(16, 16, 0.06f, 0.03f, 0.924f, true, false);
		geo = new Geometry("Bullet", cyl);
		geo.setMaterial(mat);
		//geo.setLocalTranslation(10, 10, 10); // node.attachChild(geo);
		control = new RigidBodyControl(10f);
		geo.addControl(control);
		

	}

	public void activate(Vector3f position, Vector3f direction, float speed) {
		node.attachChild(geo);
		geo.setLocalTranslation(position);
		geo.lookAt(direction, new Vector3f(0, 1, 0));
		control.setLinearVelocity(direction.normalizeLocal().multLocal(speed));
		bulletAppState.getPhysicsSpace().add(control);
		this.isInUse = true;
	}

	public void deactivate() {
		node.detachChild(geo);
		bulletAppState.getPhysicsSpace().remove(control);
		this.isInUse = false;
	}
	
	public boolean isInUse()
	{
		return this.isInUse;
	}
}
